package com.mercury.ec.module.air.service.demandorder;

import com.mercury.ec.module.air.controller.admin.demandorder.vo.demandroute.AirDemandRoutePageReqVO;
import com.mercury.ec.module.air.controller.admin.demandorder.vo.demandroute.AirDemandRouteSaveReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import com.mercury.ec.module.air.dal.dataobject.demandorder.AirDemandRouteDO;
import com.mercury.ec.framework.common.pojo.PageResult;
import com.mercury.ec.framework.common.util.object.BeanUtils;

import com.mercury.ec.module.air.dal.mysql.demandorder.AirDemandRouteMapper;

import static com.mercury.ec.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.mercury.ec.module.air.enums.ErrorCodeConstants.*;

/**
 * 机票需求订单方案行程 Service 实现类
 *
 * @author hesx
 */
@Service
@Validated
public class AirDemandRouteServiceImpl implements AirDemandRouteService {

    @Resource
    private AirDemandRouteMapper demandRouteMapper;

    @Override
    public Long createDemandRoute(AirDemandRouteSaveReqVO createReqVO) {
        // 插入
        AirDemandRouteDO demandRoute = BeanUtils.toBean(createReqVO, AirDemandRouteDO.class);
        demandRouteMapper.insert(demandRoute);
        // 返回
        return demandRoute.getId();
    }

    @Override
    public void updateDemandRoute(AirDemandRouteSaveReqVO updateReqVO) {
        // 校验存在
        validateDemandRouteExists(updateReqVO.getId());
        // 更新
        AirDemandRouteDO updateObj = BeanUtils.toBean(updateReqVO, AirDemandRouteDO.class);
        demandRouteMapper.updateById(updateObj);
    }

    @Override
    public void deleteDemandRoute(Long id) {
        // 校验存在
        validateDemandRouteExists(id);
        // 删除
        demandRouteMapper.deleteById(id);
    }

    private void validateDemandRouteExists(Long id) {
        if (demandRouteMapper.selectById(id) == null) {
            throw exception(AIR_DEMAND_ROUTE_NOT_EXISTS);
        }
    }

    @Override
    public AirDemandRouteDO getDemandRoute(Long id) {
        return demandRouteMapper.selectById(id);
    }

    @Override
    public PageResult<AirDemandRouteDO> getDemandRoutePage(AirDemandRoutePageReqVO pageReqVO) {
        return demandRouteMapper.selectPage(pageReqVO);
    }

}
